import 'package:flutter/material.dart';
import 'package:wechat_clone/constants.dart';
import 'conversation_page.dart';
import 'addressbook_page.dart';
import 'contacts_page.dart';

enum ActionItems { GROUP_CHAT, ADD_FRIEND, QR_SCAN, PAYMENT, HELP }

class NavigationIconView {
  NavigationIconView(
      {Key key, String title, IconData icon, IconData activeIcon})
      : _title = title,
        _icon = icon,
        _activeIcon = activeIcon,
        item = BottomNavigationBarItem(
          icon: Icon(icon,),
          activeIcon: Icon(activeIcon),
          title: Text(
            title,
            style: TextStyle(
              fontSize: 14.0,
              // color: Colors.red,
            ),
          ),
          // backgroundColor: Colors.red,
        );

  //定义私有变量
  final String _title;
  final IconData _icon;
  final IconData _activeIcon;

  final BottomNavigationBarItem item;
}

class HomeScreen extends StatefulWidget {
  @override
  _HomeScreenState createState() => _HomeScreenState();
}

class _HomeScreenState extends State<HomeScreen> {
  PageController _pageController;

  int _currentIndex = 0;
  List<NavigationIconView> _navigationViews;
  List<Widget> _pages;

  //该方法只会走一次，用于初始化一些只做一次的数据
  @override
  void initState() {
    super.initState();
    _pages = [
      // chatListView(),
      ConversationPage(),
      ContactsPage(),
      AddressBookPage(),
      
      Container(
        color: Colors.purple,
      ),
    ];
    _pageController = PageController(
      initialPage: _currentIndex,
    );
    _navigationViews = [
      NavigationIconView(
        title: '微信',
        icon: IconData(
          0xe608,
          fontFamily: Constants.IconFontFamily,
        ),
        activeIcon: IconData(
          0xe603,
          fontFamily: Constants.IconFontFamily,
        ),
      ),
      NavigationIconView(
        title: '通讯录',
        icon: IconData(
          0xe601,
          fontFamily: Constants.IconFontFamily,
        ),
        activeIcon: IconData(
          0xe656,
          fontFamily: Constants.IconFontFamily,
        ),
      ),
      NavigationIconView(
        title: '发现',
        icon: IconData(
          0xe600,
          fontFamily: Constants.IconFontFamily,
        ),
        activeIcon: IconData(
          0xe671,
          fontFamily: Constants.IconFontFamily,
        ),
      ),
      NavigationIconView(
        title: '我',
        icon: IconData(
          0xe6c0,
          fontFamily: Constants.IconFontFamily,
        ),
        activeIcon: IconData(
          0xe626,
          fontFamily: Constants.IconFontFamily,
        ),
      ),
    ];
  }

  _buildPopupMenuItem(int icon, String title) {
    return Container(
      child: Row(
        mainAxisAlignment: MainAxisAlignment.start,
        children: <Widget>[
          Icon(
            IconData(
              icon,
              fontFamily: Constants.IconFontFamily,
            ),
            color: Colors.white,
          ),
          SizedBox( width: 12.0,),
          Text(
            title,
            style: TextStyle(color: Colors.white),
          ),
        ],
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    final BottomNavigationBar botNavBar = BottomNavigationBar(
       fixedColor: Colors.blue,
      // fixedColor: Color.fromARGB(255, 0, 203, 32),
      items: _navigationViews.map((NavigationIconView view) {
        return view.item;
      }).toList(),
      currentIndex: _currentIndex,
      type: BottomNavigationBarType.fixed,
      onTap: (int index) {
        setState(() {
          _currentIndex = index;
          _pageController.jumpToPage(_currentIndex); //不带动画效果
          //  _pageController.animateToPage(_currentIndex, duration: Duration( milliseconds: 200), curve: Curves.linear);
        });
        print('点击的第$index个Tab。');
      },
    );
    return Scaffold(
      appBar: AppBar(
        title: Text(
          '微信(73)',
          style: TextStyle(fontSize: 20.0),
        ),
        elevation: 0, //去除阴影
        // backgroundColor: Color(0xff303030),
        actions: <Widget>[
          Container(
            padding: const EdgeInsets.only(right: 20.0),
            child: IconButton(
              icon: Icon(
                IconData(
                  0xe65e,
                  fontFamily: Constants.IconFontFamily,
                ),
                size: 22.0,
              ),
              onPressed: () {
                print('点击了搜索按钮');
                Navigator.push(context,
                    MaterialPageRoute<void>(builder: (BuildContext context) {
                  return Scaffold(
                    appBar: AppBar(
                      title: Text('Search'),
                    ),
                    body: Center(
                      child: FlatButton(
                        child: Text('Back'),
                        onPressed: () {
                          Navigator.pop(context);
                        },
                      ),
                    ),
                  );
                }));

                // Navigator.push(context, PageRouteBuilder(
                //   opaque: false,
                //   pageBuilder: ( BuildContext context, _, __){
                //     return Center( child: Text('My PageRoute'),);
                //   } ,
                //   transitionsBuilder: (___, Animation<double> animation, ____, Widget child){
                //     return FadeTransition(
                //       opacity: animation,
                //       child: RotationTransition(
                //         turns: Tween<double>( begin: 0.5, end: 1.0).animate( animation),
                //         child: child,
                //       ),
                //     );
                //   },
                // ));
              },
            ),
          ),
          PopupMenuButton(
            padding: const EdgeInsets.only(right: 20.0),
            onSelected: (ActionItems selected) {
              print('点击的是$selected');
            },
            offset: Offset(0, 64),
            icon: Icon(IconData(
              0xe658,
              fontFamily: Constants.IconFontFamily,
            )),
            itemBuilder: (BuildContext context) {
              return <PopupMenuItem<ActionItems>>[
                PopupMenuItem(
                  child: _buildPopupMenuItem(0xe69e, '发起群聊'),
                  value: ActionItems.GROUP_CHAT,
                ),
                PopupMenuItem(
                  child: _buildPopupMenuItem(0xe638, '添加朋友'),
                  value: ActionItems.ADD_FRIEND,
                ),
                PopupMenuItem(
                  child: _buildPopupMenuItem(0xe61b, '扫一扫'),
                  value: ActionItems.QR_SCAN,
                ),
                PopupMenuItem(
                  child: _buildPopupMenuItem(0xe62a, '收付款'),
                  value: ActionItems.PAYMENT,
                ),
                PopupMenuItem(
                  child: _buildPopupMenuItem(0xe63d, '帮助与反馈'),
                  value: ActionItems.HELP,
                ),
        
              ];
            },
          ),
        ],
      ),
      body: PageView.builder(
        itemBuilder: (BuildContext context, int index) {
          return _pages[index];
        },
        controller: _pageController,
        itemCount: 4,
        onPageChanged: (int index) {
          print('当前在第$index页');
          setState(() {
            _currentIndex = index;
          });
        },
      ),
      bottomNavigationBar: botNavBar,
    );
  }
}
